package cn.maxwell.controller;

import cn.maxwell.User;
import cn.maxwell.fallback.OrderUserFallback;
import cn.maxwell.feiclient.UserFeiGinClient;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 主配置类增加@EnableFeignClients标签 , 其value属性可以指定Feign的客户端接口的包,当然也可以省略value属性
 */
@RestController
public class OrderController {
    @Autowired
    private UserFeiGinClient userFeiGinClient;

    @GetMapping("/loadById/{id}")
    public User loadById(@PathVariable("id") Long id){
        return new User(1,"我自己的调用呢","try一try");
    }

    //浏览器来掉
    @RequestMapping("/order/{id}")
    //熔断
    //@SentinelResource(value="order-service",fallback = "fallbackUser",fallbackClass = OrderUserFallback.class)
    public User getById(@PathVariable("id")Long id){
        //使用Feign调用用户服务获取User
        return userFeiGinClient.loadById(id);
    }

    /**
     * 熔断兜底方法
     * @param id
     * @return
     */
    public User fallbackUser(@PathVariable("id") Long id,Throwable th){
        return new User(-1,"被熔断了","请稍后尝试！！！");
    }

}
